//package com.level.system.controller.EMR;
//
//
//import com.level.system.entity.DTO.UserDto;
//import com.level.system.service.UserService;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.subject.Subject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.http.*;
//import org.springframework.web.bind.annotation.*;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//@RestController
//public class LoginController {
//
//	private Logger logger = LoggerFactory.getLogger(LoginController.class);
//
//    private UserService userService;
//
//    public LoginController(UserService userService) {
//    	this.userService = userService;
//    }
//
//    /**
//     * 用户名密码登录
//     * @param request
//     * @return token
//     */
//    @RequestMapping(value = "/login")
//    public ResponseEntity<Void> login(UserDto loginInfo, HttpServletRequest request, HttpServletResponse response){
//        Subject subject = SecurityUtils.getSubject();
//        try {
//            UsernamePasswordToken token = new UsernamePasswordToken(loginInfo.getUsername(), loginInfo.getPassword());
//            subject.login(token);
//
//            UserDto user = (UserDto) subject.getPrincipal();
//            String newToken = userService.generateJwtToken(user.getUsername());
//            response.setHeader("x-auth-token", newToken);
//
//            return ResponseEntity.ok().build();
//        } catch (AuthenticationException e) {
//            logger.error("User {} login fail, Reason:{}", loginInfo.getUsername(), e.getMessage());
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
//        } catch (Exception e) {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
//        }
//    }
//
//    /**
//     * 退出登录
//     * @return
//     */
//    @GetMapping(value = "/logout")
//    public ResponseEntity<Void> logout() {
//    	Subject subject = SecurityUtils.getSubject();
//        if(subject.getPrincipals() != null) {
//            UserDto user = (UserDto)subject.getPrincipals().getPrimaryPrincipal();
//            userService.deleteLoginInfo(user.getUsername());
//        }
//        SecurityUtils.getSubject().logout();
//        return ResponseEntity.ok().build();
//    }
//
//}
